package cn.stylefeng.guns.modular.business.controller;

import cn.stylefeng.guns.modular.business.entity.WxUser;
import cn.stylefeng.guns.modular.business.pojo.WxUserRequest;
import cn.stylefeng.guns.modular.business.service.WxUserService;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.bouncycastle.asn1.iso.ISOIECObjectIdentifiers;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 车辆管理控制器
 *
 * @author 施律名
 * @date 2022-03-16 16:44:29
 */
@RestController
@ApiResource(name = "微信用户管理")
public class WxUserController {

    @Resource
    private WxUserService wxUserService;

    @PostResource(name = "使用code获取openId",path = "/wx/user/loing")
    public WxUser wxlogin(@RequestBody String code) throws IOException {
        System.out.println("123123");
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx1deefccf050687b7&secret=4ae6e30fe1ea26a0f72f27f0385d1aa3&js_code="+code+"&grant_type=authorization_code";
        System.out.println(code);
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Response response = client.newCall(request).execute();
        if (response.isSuccessful()){
            String body = response.body().string();
            System.out.println(body);
        }
        System.out.println(code);
        return new WxUser();
    }

    /**
     * 添加用户
     *
     * @author 施律名
     * @date 2022-03-16 16:44:29
     */
    @PostResource(name = "添加用户", path = "/wx/user/add")
    public ResponseData add(@RequestBody @Validated(WxUserRequest.add.class) WxUserRequest wxUserRequest) {
        wxUserService.add(wxUserRequest);
        return new SuccessResponseData();
    }

    /**
     * 删除车辆
     *
     * @author 施律名
     * @date 2022-03-16 16:44:29
     */
    @PostResource(name = "删除用户", path = "/wx/user/delete")
    public ResponseData delete(@RequestBody @Validated(WxUserRequest.delete.class) WxUserRequest wxUserRequest) {
        wxUserService.del(wxUserRequest);
        return new SuccessResponseData();
    }

    /**
     * 编辑车辆
     *
     * @author 施律名
     * @date 2022-03-16 16:44:29
     */
    @PostResource(name = "编辑信息", path = "/wx/user/edit")
    public ResponseData edit(@RequestBody @Validated(WxUserRequest.edit.class) WxUserRequest wxUserRequest) {
        wxUserService.edit(wxUserRequest);
        return new SuccessResponseData();
    }

    /**
     * 查看车辆详情
     *
     * @author 施律名
     * @date 2022-03-16 16:44:29
     */
    @GetResource(name = "查看详情", path = "/wx/user/detail")
    public ResponseData detail(@Validated(WxUserRequest.detail.class) WxUserRequest wxUserRequest) {
        return new SuccessResponseData(wxUserService.detail(wxUserRequest));
    }

    /**
     * 查询车辆列表
     *
     * @author 施律名
     * @date 2022-03-16 16:44:29
     */
    @GetResource(name = "获取用户列表", path = "/wx/user/findList")
    public ResponseData list(WxUserRequest wxUserRequest) {
        return new SuccessResponseData(wxUserService.findList(wxUserRequest));
    }

    /**
     * 分页查询车辆列表
     *
     * @author 施律名
     * @date 2022-03-16 16:44:29
     */
    @GetResource(name = "分页查询", path = "/wx/user/findPage")
    public ResponseData page(WxUserRequest wxUserRequest) {
        return new SuccessResponseData(wxUserService.findPage(wxUserRequest));
    }

}